﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using YABE.Models;
using YABE.Validators;

namespace YABETests.ValidationTests
{
    /// <summary>
    /// Summary description for CommentValidations
    /// </summary>
    [TestClass]
    public class CommentValidations
    {
        public CommentValidations()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod()]
        public void CommentValidationShouldReturn_EmailNull_WhenEmailIsNullOrEmpty()
        {
            Comment comment = new Comment();
            RuleViolation emailViolation = comment.ValidateEmailNotNull();
            Assert.IsNotNull(emailViolation);
            Assert.AreEqual("EmailNull",emailViolation.PropertyName);
            Assert.AreEqual(ErrorMessages.EmailIsNull,emailViolation.ErrorMessage);
        }

        [TestMethod()]
        public void CommentValidationShouldReturn_NULL_WhenEmailIsNotNull()
        {
            Comment comment = new Comment();
            comment.Email = "test";
            RuleViolation emailViolation = comment.ValidateEmailNotNull();
            Assert.IsNull(emailViolation);
        }

        [TestMethod()]
        public void CommentValidationShouldReturn_EmailPattern_WhenEmailFormatIsInvalid()
        {
            Comment comment = new Comment();
            comment.Email = "test";
            RuleViolation emailViolation = comment.ValidateEmailFormat();
            Assert.IsNotNull(emailViolation);
            Assert.AreEqual("EmailPattern", emailViolation.PropertyName);
            Assert.AreEqual("test",emailViolation.PropertyValue);
            Assert.AreEqual(ErrorMessages.EmailPattern, emailViolation.ErrorMessage);
        }

        [TestMethod()]
        public void CommentValidationShouldReturn_NoError_WhenEmailIsValid()
        {
            Comment comment = new Comment();
            comment.Email = "test@test.com";
            RuleViolation emailViolation = comment.ValidateEmailFormat();
            Assert.IsNull(emailViolation);
        }

        [TestMethod()]
        public void CommentValidationShouldReturn_AuthorEmpty_WhenAuthorIsEmpty()
        {
            Comment comment = new Comment();
            RuleViolation authorViolation = comment.CheckAuthorNotEmpty();
            Assert.IsNotNull(authorViolation);
            Assert.AreEqual("AuthorEmpty",authorViolation.PropertyName);
            Assert.AreEqual(ErrorMessages.AuthorEmpty,authorViolation.ErrorMessage);
        }

        [TestMethod()]
        public void CommentValidationShouldReturn_NoError_WhenAuthorIsNOTEmpty()
        {
            Comment comment = new Comment();
            comment.Author = "Test";
            RuleViolation authorViolation = comment.CheckAuthorNotEmpty();
            Assert.IsNull(authorViolation);
        }

        [TestMethod()]
        public void CommentValidationShouldReturn_CommentsEmpty_WhenCommentsIsEmpty()
        {
            Comment comment = new Comment();
            RuleViolation commentViolation = comment.CheckCommentsNotEmpty();
            Assert.IsNotNull(commentViolation);
            Assert.AreEqual("CommentsEmpty",commentViolation.PropertyName);
            Assert.AreEqual(ErrorMessages.CommentsEmpty,commentViolation.ErrorMessage);
        }
        [TestMethod()]
        public void CommentValidationShouldReturn_NoError_WhenCommentsIsNOTEmpty()
        {
            Comment comment = new Comment();
            comment.Comments = "test";
            RuleViolation commentViolation = comment.CheckCommentsNotEmpty();
            Assert.IsNull(commentViolation);
        }

        [TestMethod()]
        public void CommentValidationShouldReturn_FrequentCommentor_WhenUserCommentsBefore30Secs()
        {
            MockRepository mockRepository = new MockRepository();
            ICommentDL commentDL = mockRepository.Stub<ICommentDL>();
            Comment latestComment = new Comment();
            latestComment.Created = DateTime.Now.AddSeconds(5);
            using (mockRepository.Record())
            {
                SetupResult.For(commentDL.GetLatestCommentByCommentor("test")).Return(latestComment);
            }

            Comment newComment = new Comment();
            newComment.ClientIP = "test";
            newComment.commentDL = commentDL;
            RuleViolation commentorFrequencyValidation = newComment.CheckCommentorFrequency();
            Assert.IsNotNull(commentorFrequencyValidation);
            Assert.AreEqual("FrequentCommentor",commentorFrequencyValidation.PropertyName);
            Assert.AreEqual(ErrorMessages.FrequentCommentor,commentorFrequencyValidation.ErrorMessage);
        }

        [TestMethod()]
        public void CommentValidationShouldReturn_NoError_WhenUserCommented1MinuteAgo()
        {
            MockRepository mockRepository = new MockRepository();
            ICommentDL commentDL = mockRepository.Stub<ICommentDL>();
            Comment latestComment = new Comment();
            latestComment.Created = DateTime.Now.Subtract(new TimeSpan(0,0,1,0));
            using (mockRepository.Record())
            {
                SetupResult.For(commentDL.GetLatestCommentByCommentor("test")).Return(latestComment);
            }

            Comment newComment = new Comment();
            newComment.ClientIP = "test";
            newComment.commentDL = commentDL;
            RuleViolation commentorFrequencyValidation = newComment.CheckCommentorFrequency();
            Assert.IsNull(commentorFrequencyValidation);
        }

        [TestMethod()]
        public void CommentValidationsShouldReturn_5Violations()
        {
            MockRepository mockRepository = new MockRepository();
            ICommentDL commentDL = mockRepository.Stub<ICommentDL>();
            Comment latestComment = new Comment();
            latestComment.Created = DateTime.Now.AddSeconds(5);
            using (mockRepository.Record())
            {
                SetupResult.For(commentDL.GetLatestCommentByCommentor("test")).Return(latestComment);
            }

            Comment newComment = new Comment();
            newComment.ClientIP = "test";
            newComment.commentDL = commentDL;
            List<RuleViolation> violations = newComment.GetRuleViolations();
            Assert.IsNotNull(violations);
            Assert.AreEqual(5,violations.Count);
        }
    }
}